******************************************************************
*Input: FinTech/rejected borrowers dataset
*Output: Stata graph with share of firms per rating
******************************************************************

clear 
use "$output\rejected_agg_industries_modified.dta", clear

keep if deltamonths == -11

merge m:1 siren using "$output\list_siren_pretup.dta"
keep if accepted & _merge == 3 | !accepted
drop _merge

egen ratingint = group(rating)
replace ratingint = ratingint - 1
replace ratingint = 13 if ratingint==0
	
gen code = 1 if ratingint == 1
replace code = 2 if ratingint == 2
replace code = 3 if ratingint == 3
replace code = 4 if ratingint == 4
replace code = 5 if ratingint == 5
replace code = 6 if ratingint == 6
replace code = 7 if ratingint == 7
replace code = 8 if ratingint == 8
replace code = 9 if ratingint == 9
replace code = 10 if ratingint == 10
replace code = 11 if ratingint == 11 | rating == 12
replace code = 12 if ratingint == 13

drop if code == .
gen rejected = accepted == 0

keep code rejected accepted
rename (rejected accepted) (rejected accepted)

collapse (sum) rejected accepted,by(code)

sort code
egen tot_acc = total(accepted)
egen tot_rej= total(rej)

replace rejected = rejected/tot_rej*100
replace accepted = accepted/tot_acc*100

label var rejected "Rejected FinTech borrowers"
label var accepted "Accepted FinTech borrowers"

graph hbar accepted rejected, over(code, gap(*1)  ///
relabel(1 "3++"  ///
2 "3+" ///
3 "3" ///
4 "4+" ///
5 "4" ///
6 "5+" ///
7 "5" ///
8 "6" ///
9 "7" ///
10 "8" ///
11 "P/9" ///
12 "Unrated" ///
) label(labsize(medium)) ) ///
graphregion(color(white)) blabel(bar, format(%8.1f)   )nolabel ///
 bar(1, bcolor(emerald)) bar(2, bcolor(gs10%40)  lcolor(black))  ///
legend( region(lcolor(white) )  lcolor(white) label(1 "Fintech borrowers") label(2 "Rejected borrowers") cols(2) size(small) ) ylabel(,nogrid labsize(medium))

graph export "$figures/ratings_rejected.pdf", as(pdf) replace 
graph save "$figures/ratings_rejected.gph", replace 


